//
//  WHForgetPasswordController.m
//  SquirrelCourier
//
//  Created by 邓伟浩 on 2019/1/3.
//  Copyright © 2019 邓伟浩. All rights reserved.
//

#import "WHForgetPasswordController.h"
#import "WHForgetPasswordView.h"
#import "NSString+WH3DES.h"

@interface WHForgetPasswordController ()

@property (nonatomic, strong) NSURLSessionDataTask *resetPasswordTask;
@property (nonatomic, strong) WHForgetPasswordView *forgetPasswordView;


@end

@implementation WHForgetPasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"忘记密码";
    [self setUpUI];
}

- (void)setUpUI {
    WHForgetPasswordView *forgetPasswordView = [[WHForgetPasswordView alloc] init];
    [self.view addSubview:forgetPasswordView];
    [forgetPasswordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view.mas_top).offset(kTopHeight);
    }];
    kWeakSelf(self);
    forgetPasswordView.doResetPassword = ^(NSArray * _Nonnull messageArray) {
        [weakself doResetPassword:messageArray];
    };
    _forgetPasswordView = forgetPasswordView;

}

- (void)doResetPassword:(NSArray *)messageArray {
    DLog(@"doResetPassword = %@", messageArray);
    [self.view endEditing:YES];
    
    NSString *phoneNumber = messageArray[0];
    NSString *codeNumber = messageArray[1];
    NSString *password = messageArray[2];
    NSString *checkpassword = messageArray[3];
    if (phoneNumber.length == 0 || codeNumber.length == 0 || password.length == 0 || checkpassword.length == 0) {
        [WHView showMessage:@"请完善以上信息" inView:self.view];
        return;
    }
    if (codeNumber.length != 6) {
        [WHView showMessage:@"请输入6位验证码" inView:self.view];
        return;
    }
    if (![WHValidationHelper validatePassword:password]) {
        [WHView showMessage:@"密码输入不合法!" inView:self.view];
        return;
    }
    if (![password isEqualToString:checkpassword]) {
        [WHView showMessage:@"两次输入密码不一致，请重新输入" inView:self.view];
        return;
    }
    
    kWeakSelf(self);
    [self doForgetPassword:messageArray completion:^(id responseObject) {
        if (ValidDict(responseObject)) {
            NSNumber *code = responseObject[@"errcode"];
            NSString *errorMsg = responseObject[@"errmsg"];
            if (code.intValue == 0) {
                [WHView showHudMsg:@"修改密码成功"];
                [weakself.navigationController popViewControllerAnimated:YES];
            } else {
                [WHView showHudMsg:errorMsg];
            }
        }
    }];
    
}

#pragma mark —————— 接口 ——————

/** 修改密码 */
- (void)doForgetPassword:(NSArray *)messageArray completion:(completionBlock)completion {
    NSString *postURL = NSStringFormat(@"%@%@", URL_main, URL_forgetPassWord);
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          messageArray[0], @"userCell",
                          messageArray[1], @"smsCode",
                          [NSString encryptUseDES3:messageArray[2] key:DES3Key], @"passWord",
                          @"1", @"system",
                          @"1", @"forgetType",
                          nil];
    
    _resetPasswordTask = [WHNetworkManager requestWithURL:postURL parameters:dict isUpData:YES Success:^(id responseObject) {
        DLog(@"%@", responseObject);
        completion(responseObject);
    } failure:^(NSError *error) {
        
    }];
}

- (void)backBtnClicked {
    [super backBtnClicked];
    if (_resetPasswordTask) {
        [_resetPasswordTask cancel];
        _resetPasswordTask = nil;
    }
    [_forgetPasswordView cancelRequest];
}

@end
